home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / ispell40.lha / ispell-4.0 / mklextbl.c < prev    next >
C/C++ Source or Header  |  1993-05-31  |  4KB  |  192 lines

  1. /* Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU ISPELL.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21.  
  22. #ifdef HAVE_MALLOC_H
  23. #include <malloc.h>
  24. #endif
  25.  
  26. #include "ispell.h"
  27. #include "charset.h"
  28.  
  29. int lexletters;
  30. unsigned char **lextable;
  31. unsigned char *lexchar;
  32. char **lexdecode;
  33.  
  34. extern char *freqletters;
  35.  
  36. void
  37. mklextbl (name, outf)
  38.   char *name;
  39.   FILE *outf;
  40. {
  41.   FILE *f;
  42.   int i, j;
  43.   char buf[300], *p, *q;
  44.   int code;
  45.  
  46.   if ((f = fopen (name, "r")) == NULL)
  47.     {
  48.       (void) fprintf (stderr, "can't open %s\n", name);
  49.       exit (1);
  50.     }
  51.  
  52.   if (fgets (buf, sizeof buf, f) == NULL)
  53.     goto badfreq;
  54.  
  55.   if ((p = (char *) strchr (buf, ' ')) == NULL)
  56.     goto badfreq;
  57.   p++;
  58.   if ((q = (char *) strchr (buf, '\n')) != NULL)
  59.     *q = 0;
  60.  
  61.   if ((freqletters = (char *) xmalloc (strlen (p) + 1)) == NULL)
  62.     {
  63.       fprintf (stderr, "out of memory for freqletters\n");
  64.       exit (1);
  65.     }
  66.  
  67.   strcpy (freqletters, p);
  68.  
  69.   lexletters = strlen (freqletters) + 1;
  70.  
  71.   lexalloc ();
  72.  
  73.   code = 1;
  74.   for (p = freqletters; *p && *p != '\n'; p++)
  75.     {
  76.       lexchar[*p & 0xff] = code;
  77.       q = lexdecode[code];
  78.       *q++ = *p;
  79.       *q = 0;
  80.       code++;
  81.     }
  82.  
  83.   for (i = 0; i < 256; i++)
  84.     {
  85.       if (lexchar[charset[i].lowercase])
  86.     lexchar[i] = lexchar[charset[i].lowercase];
  87.     }
  88.  
  89.   while (code < 255 && fgets (buf, sizeof buf, f) != NULL)
  90.     {
  91.       p = buf;
  92.       while (isspace (*p))
  93.     p++;
  94.       while (*p && !isspace (*p))
  95.     p++;
  96.       while (isspace (*p))
  97.     p++;
  98.       q = lexdecode[code];
  99.       *q++ = *p++;
  100.       *q++ = *p;
  101.       *q = 0;
  102.       code++;
  103.     }
  104.   (void) fclose (f);
  105.  
  106.   if (code != 255)
  107.     {
  108.       (void) fprintf (stderr,
  109.               "freqfile '%s' does not have enough entries\n", name);
  110.       exit (1);
  111.     }
  112.  
  113.   for (i = 0; i < 256; i++)
  114.     {
  115.       p = lexdecode[i];
  116.       if (p[1])
  117.     lextable[lexchar[p[0]]][lexchar[p[1]]] = i;
  118.     }
  119.  
  120.   for (i = 0; i < lexletters; i++)
  121.     {
  122.       lextable[i][0] = 255;
  123.       lextable[0][i] = 255;
  124.     }
  125.  
  126.   if (outf == NULL)
  127.     return;
  128.  
  129.   /* the following is old stuff when the lex table was written
  130.      * to a c file, and then compiled into other programs.  It's
  131.      * still here because it is sometimes helpful when debugging.
  132.      */
  133.   (void) fprintf (outf, "char *lexdecode[256] = {");
  134.   for (i = 0; i < 256; i++)
  135.     {
  136.       if (i % 8 == 0)
  137.     (void) fprintf (outf, "\n/*%3d*/\t", i);
  138.       switch (strlen (lexdecode[i]))
  139.     {
  140.     case 0:
  141.       (void) fprintf (outf, "  ");
  142.       break;
  143.     case 1:
  144.       (void) fprintf (outf, " ");
  145.       break;
  146.     }
  147.       (void) fprintf (outf, "\"%s\", ", lexdecode[i]);
  148.     }
  149.   (void) fprintf (outf, "\n};\n");
  150.   (void) fprintf (outf, "\n");
  151.  
  152.   (void) fprintf (outf, "unsigned char lexchar[256] = {");
  153.   for (i = 0; i < 256; i++)
  154.     {
  155.       if (i % 8 == 0)
  156.     (void) fprintf (outf, "\n\t");
  157.       (void) fprintf (outf, "%3d, ", lexchar[i]);
  158.     }
  159.   (void) fprintf (outf, "\n};\n");
  160.   (void) fprintf (outf, "\n");
  161.  
  162.   (void) fprintf (outf, "unsigned char lextable[%d][%d] = {\n",
  163.           lexletters, lexletters);
  164.   for (i = 0; i < lexletters; i++)
  165.     {
  166.       (void) fprintf (outf, "\t{ ");
  167.       for (j = 0; j < lexletters; j++)
  168.     {
  169.       (void) fprintf (outf, "%3d, ", lextable[i][j]);
  170.       if (j == 10 || j == 21)
  171.         (void) fprintf (outf, "\n\t  ");
  172.     }
  173.       (void) fprintf (outf, "},\n");
  174.     }
  175.   (void) fprintf (outf, "};\n");
  176.  
  177.   (void) fclose (outf);
  178.   return;
  179. badfreq:
  180.   fprintf (stderr, "bad freq table\n");
  181.   exit (1);
  182. }
  183.  
  184. #ifdef TEST
  185. main (argc, argv)
  186.      char **argv;
  187. {
  188.   mklextbl (argv[1], stdout);
  189. }
  190.  
  191. #endif
  192.